﻿using System;
using System.Collections.Generic;
using System.Linq;
using UniRx;
using UniRx.Triggers;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace Studio
{
	// Token: 0x02000574 RID: 1396
	public class VoiceControl : MonoBehaviour
	{
		// Token: 0x1700044E RID: 1102
		// (set) Token: 0x060023A3 RID: 9123 RVA: 0x000D69FC File Offset: 0x000D4BFC
		public OCIChar ociChar
		{
			set
			{
				this.m_OCIChar = value;
				if (this.m_OCIChar != null)
				{
					this.InitList();
				}
			}
		}

		// Token: 0x1700044F RID: 1103
		// (get) Token: 0x060023A4 RID: 9124 RVA: 0x000D6A18 File Offset: 0x000D4C18
		// (set) Token: 0x060023A5 RID: 9125 RVA: 0x000D6A28 File Offset: 0x000D4C28
		public bool active
		{
			get
			{
				return base.gameObject.activeSelf;
			}
			set
			{
				if (base.gameObject.activeSelf != value)
				{
					base.gameObject.SetActive(value);
				}
			}
		}

		// Token: 0x060023A6 RID: 9126 RVA: 0x000D6A48 File Offset: 0x000D4C48
		public void InitList()
		{
			for (int i = 0; i < this.transformRoot.childCount; i++)
			{
				UnityEngine.Object.Destroy(this.transformRoot.GetChild(i).gameObject);
			}
			this.transformRoot.DetachChildren();
			this.select = -1;
			this.listNode.Clear();
			foreach (VoiceCtrl.VoiceInfo voiceInfo in this.m_OCIChar.voiceCtrl.list)
			{
				Info.LoadCommonInfo voiceInfo2 = Singleton<Info>.Instance.GetVoiceInfo(voiceInfo.group, voiceInfo.category, voiceInfo.no);
				if (voiceInfo2 == null)
				{
					GlobalData.Debug_Log(string.Format("得不到情报 G[{0}] : C[{1}] : N[{2}]", voiceInfo.group, voiceInfo.category, voiceInfo.no));
				}
				else
				{
					this.AddNode(voiceInfo2.name);
				}
			}
			this.scrollRect.verticalNormalizedPosition = 1f;
			this.imagePlayNow.enabled = (this.m_OCIChar != null && this.m_OCIChar.voiceCtrl.isPlay);
			this.buttonRepeat.image.sprite = this.spriteRepeat[(int)this.m_OCIChar.voiceRepeat];
			this.voiceRegistrationList.ociChar = this.m_OCIChar;
		}

		// Token: 0x060023A7 RID: 9127 RVA: 0x000D6BCC File Offset: 0x000D4DCC
		private void AddNode(string _name)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.objectNode);
			gameObject.transform.SetParent(this.transformRoot, false);
			VoicePlayNode vpn = gameObject.GetComponent<VoicePlayNode>();
			vpn.active = true;
			vpn.addOnClick = delegate()
			{
				this.OnClickSelect(vpn);
			};
			vpn.addOnClickDelete = delegate()
			{
				this.OnClickDelete(vpn);
			};
			vpn.text = _name;
			this.listNode.Add(vpn);
		}

		// Token: 0x060023A8 RID: 9128 RVA: 0x000D6C68 File Offset: 0x000D4E68
		private void OnClickRepeat()
		{
			int num = Enum.GetNames(typeof(VoiceCtrl.Repeat)).Length;
			int num2 = (int)this.m_OCIChar.voiceRepeat;
			num2 = (num2 + 1) % num;
			this.m_OCIChar.voiceRepeat = (VoiceCtrl.Repeat)num2;
			this.buttonRepeat.image.sprite = this.spriteRepeat[num2];
		}

		// Token: 0x060023A9 RID: 9129 RVA: 0x000D6CC0 File Offset: 0x000D4EC0
		private void OnClickStop()
		{
			this.m_OCIChar.StopVoice();
		}

		// Token: 0x060023AA RID: 9130 RVA: 0x000D6CD0 File Offset: 0x000D4ED0
		private void OnClickPlay()
		{
			bool enabled = this.m_OCIChar.PlayVoice(this.select);
			this.imagePlayNow.enabled = enabled;
		}

		// Token: 0x060023AB RID: 9131 RVA: 0x000D6CFC File Offset: 0x000D4EFC
		private void OnClickPlayAll()
		{
			OCIChar[] array = (from v in Singleton<GuideObjectManager>.Instance.selectObjectKey
			select Studio.GetCtrlInfo(v) as OCIChar into v
			where v != null
			select v).ToArray<OCIChar>();
			int num = array.Length;
			for (int i = 0; i < num; i++)
			{
				array[i].PlayVoice(0);
			}
		}

		// Token: 0x060023AC RID: 9132 RVA: 0x000D6D80 File Offset: 0x000D4F80
		private void OnClickStopAll()
		{
			OCIChar[] array = (from v in Singleton<GuideObjectManager>.Instance.selectObjectKey
			select Studio.GetCtrlInfo(v) as OCIChar into v
			where v != null
			select v).ToArray<OCIChar>();
			int num = array.Length;
			for (int i = 0; i < num; i++)
			{
				array[i].StopVoice();
			}
		}

		// Token: 0x060023AD RID: 9133 RVA: 0x000D6E00 File Offset: 0x000D5000
		private void OnClickExpansion()
		{
			bool flag = !this.objBeneath.activeSelf;
			this.objBeneath.SetActive(flag);
			this.buttonExpansion.image.sprite = this.spriteExpansion[(!flag) ? 0 : 1];
		}

		// Token: 0x060023AE RID: 9134 RVA: 0x000D6E4C File Offset: 0x000D504C
		private void OnClickSave()
		{
			this.voiceRegistrationList.active = !this.voiceRegistrationList.active;
			if (this.voiceRegistrationList.active)
			{
				this.voiceRegistrationList.ociChar = this.m_OCIChar;
			}
		}

		// Token: 0x060023AF RID: 9135 RVA: 0x000D6E88 File Offset: 0x000D5088
		private void OnClickDeleteAll()
		{
			int count = this.listNode.Count;
			for (int i = 0; i < count; i++)
			{
				this.listNode[i].Destroy();
			}
			this.listNode.Clear();
			this.scrollRect.verticalNormalizedPosition = 1f;
			this.m_OCIChar.DeleteAllVoice();
		}

		// Token: 0x060023B0 RID: 9136 RVA: 0x000D6EEC File Offset: 0x000D50EC
		private void OnClickSelect(VoicePlayNode _vpn)
		{
			if (MathfEx.RangeEqualOn<int>(0, this.select, this.listNode.Count))
			{
				this.listNode[this.select].select = false;
			}
			this.select = this.listNode.FindIndex((VoicePlayNode v) => v == _vpn);
			this.listNode[this.select].select = true;
		}

		// Token: 0x060023B1 RID: 9137 RVA: 0x000D6F70 File Offset: 0x000D5170
		private void OnClickDelete(VoicePlayNode _vpn)
		{
			int num = this.listNode.FindIndex((VoicePlayNode v) => v == _vpn);
			this.listNode.RemoveAt(num);
			_vpn.Destroy();
			this.scrollRect.verticalNormalizedPosition = 1f;
			this.m_OCIChar.DeleteVoice(num);
			if (this.select == num)
			{
				this.select = -1;
			}
		}

		// Token: 0x060023B2 RID: 9138 RVA: 0x000D6FE8 File Offset: 0x000D51E8
		private void Start()
		{
			this.buttonRepeat.onClick.AddListener(new UnityAction(this.OnClickRepeat));
			this.buttonStop.onClick.AddListener(new UnityAction(this.OnClickStop));
			this.buttonPlay.onClick.AddListener(new UnityAction(this.OnClickPlay));
			this.buttonPlayAll.onClick.AddListener(new UnityAction(this.OnClickPlayAll));
			this.buttonStopAll.onClick.AddListener(new UnityAction(this.OnClickStopAll));
			this.buttonExpansion.onClick.AddListener(new UnityAction(this.OnClickExpansion));
			this.buttonSave.onClick.AddListener(new UnityAction(this.OnClickSave));
			this.buttonDeleteAll.onClick.AddListener(new UnityAction(this.OnClickDeleteAll));
			this.UpdateAsObservable().Subscribe(delegate(Unit _)
			{
				this.imagePlayNow.enabled = (this.m_OCIChar != null && this.m_OCIChar.voiceCtrl.isPlay);
			});
		}

		// Token: 0x04002493 RID: 9363
		[SerializeField]
		private GameObject objectNode;

		// Token: 0x04002494 RID: 9364
		[SerializeField]
		private Transform transformRoot;

		// Token: 0x04002495 RID: 9365
		[SerializeField]
		private ScrollRect scrollRect;

		// Token: 0x04002496 RID: 9366
		[SerializeField]
		private Button buttonRepeat;

		// Token: 0x04002497 RID: 9367
		[SerializeField]
		private Sprite[] spriteRepeat;

		// Token: 0x04002498 RID: 9368
		[SerializeField]
		private Button buttonStop;

		// Token: 0x04002499 RID: 9369
		[SerializeField]
		private Button buttonPlay;

		// Token: 0x0400249A RID: 9370
		[SerializeField]
		private Image imagePlayNow;

		// Token: 0x0400249B RID: 9371
		[SerializeField]
		private Button buttonPlayAll;

		// Token: 0x0400249C RID: 9372
		[SerializeField]
		private Button buttonStopAll;

		// Token: 0x0400249D RID: 9373
		[SerializeField]
		private Button buttonExpansion;

		// Token: 0x0400249E RID: 9374
		[SerializeField]
		private Sprite[] spriteExpansion;

		// Token: 0x0400249F RID: 9375
		[SerializeField]
		private GameObject objBeneath;

		// Token: 0x040024A0 RID: 9376
		[SerializeField]
		private Button buttonSave;

		// Token: 0x040024A1 RID: 9377
		[SerializeField]
		private Button buttonDeleteAll;

		// Token: 0x040024A2 RID: 9378
		[SerializeField]
		private VoiceRegistrationList voiceRegistrationList;

		// Token: 0x040024A3 RID: 9379
		private OCIChar m_OCIChar;

		// Token: 0x040024A4 RID: 9380
		private List<VoicePlayNode> listNode = new List<VoicePlayNode>();

		// Token: 0x040024A5 RID: 9381
		private int select = -1;
	}
}
